package com.zhanqiao.Dao;

import com.zhanqiao.Utils.GetDataSourceUtil;
import com.zhanqiao.domain.ProductBean;
import com.zhanqiao.domain.VipBean;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

public class SelectAllVipDao implements VipDao {
    @Override
    public List<VipBean> selectAll(String page, String limit) {
        DataSource ds = GetDataSourceUtil.getDataSource();
        QueryRunner queryRunner = new QueryRunner(ds);
        int pages = Integer.parseInt(page);
        int counts = Integer.parseInt(limit);
        Object[] parm = {(pages-1)*counts,counts};
        String sql = "select * from vip limit ?,?";
        try {
            List<VipBean> allVips = queryRunner.query(sql, new BeanListHandler<VipBean>(VipBean.class),parm);
            return allVips;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    public Long findCount() throws SQLException {
        DataSource ds = GetDataSourceUtil.getDataSource();
        QueryRunner queryRunner = new QueryRunner(ds);
        return queryRunner.query("select count(*) from vip", new ScalarHandler<Long>());
    }
    public Integer updataVipS( VipBean vipBean) throws SQLException {
        DataSource ds = GetDataSourceUtil.getDataSource();
        QueryRunner queryRunner = new QueryRunner(ds);
        String sql = "update vip set vname=?,vpwd=?,vsex=?,vrestmoney=?,vphone=? where vid =?";
        Object[] parm = {vipBean.getVname(), vipBean.getVpwd(),vipBean.getVsex(),vipBean.getVrestmoney(),vipBean.getVphone(),vipBean.getVid()};
        return queryRunner.update(sql, parm);
    }

    public Integer deleteVips(Integer vid) throws SQLException {
        DataSource ds = GetDataSourceUtil.getDataSource();
        QueryRunner queryRunner = new QueryRunner(ds);
        String sql = "delete from vip where vid=?";
        return queryRunner.update(sql, vid);
    }

    public Integer insertVip(VipBean vipBean) throws SQLException {
        DataSource ds = GetDataSourceUtil.getDataSource();
        QueryRunner queryRunner = new QueryRunner(ds);
        String sql = "insert into vip values(null,?,?,?,?,?)";
        Object[] parm = {vipBean.getVname(), vipBean.getVpwd(),vipBean.getVsex(),vipBean.getVrestmoney(),vipBean.getVphone()};
        return queryRunner.update(sql, parm);
    }



}
